Headless UI 您所在的位置:网站首页 transition from popular Headless UI

Headless UI

2023-11-30 17:40| 来源: 网络整理| 查看: 265

Transition

The Transition component lets you add enter/leave transitions to conditionally rendered elements, using CSS classes to control the actual transition styles in the different stages of the transition.

Installation

To get started, install Headless UI via npm:

npm install @headlessui/reactBasic example

The Transition accepts a show prop that controls whether the children should be shown or hidden, and a set of lifecycle props (like enterFrom, and leaveTo) that let you add CSS classes at specific phases of a transition.

import { Transition } from '@headlessui/react' import { useState } from 'react' function MyComponent() { const [isShowing, setIsShowing] = useState(false) return ( setIsShowing((isShowing) => !isShowing)}> Toggle I will fade in and out ) }Showing and hiding content

Wrap the content that should be conditionally rendered in a component, and use the show prop to control whether the content should be visible or hidden.

import { Transition } from '@headlessui/react' import { useState } from 'react' function MyComponent() { const [isShowing, setIsShowing] = useState(false) return ( setIsShowing((isShowing) => !isShowing)}> Toggle I will appear and disappear. ) }

The Transition component will render a div by default, but you can use the as prop to render a different element instead if needed. Any other HTML attributes (like className) can be added directly to the Transition the same way they would be to regular elements.

import { Transition } from '@headlessui/react' import { useState } from 'react' function MyComponent() { const [isShowing, setIsShowing] = useState(false) return ( setIsShowing((isShowing) => !isShowing)}> Toggle I will appear and disappear. ) } Animating transitions

By default, a Transition will enter and leave instantly, which is probably not what you're looking for if you're using this component.

To animate your enter/leave transitions, add classes that provide the styling for each phase of the transitions using these props:

enter: Applied the entire time an element is entering. Usually you define your duration and what properties you want to transition here, for example transition-opacity duration-75. enterFrom: The starting point to enter from, for example opacity-0 if something should fade in. enterTo: The ending point to enter to, for example opacity-100 after fading in. leave: Applied the entire time an element is leaving. Usually you define your duration and what properties you want to transition here, for example transition-opacity duration-75. leaveFrom: The starting point to leave from, for example opacity-100 if something should fade out. leaveTo: The ending point to leave to, for example opacity-0 after fading out.

Here's an example:

import { Transition } from '@headlessui/react' import { useState } from 'react' function MyComponent() { const [isShowing, setIsShowing] = useState(false) return ( setIsShowing((isShowing) => !isShowing)}> Toggle I will fade in and out ) }

In this example, the transitioning element will take 75ms to enter (that's the duration-75 class), and will transition the opacity property during that time (that's transition-opacity).

It will start completely transparent before entering (that's opacity-0 in the enterFrom phase), and fade in to completely opaque (opacity-100) when finished (that's the enterTo phase).

When the element is being removed (the leave phase), it will transition the opacity property, and spend 150ms doing it (transition-opacity duration-150).

It will start as completely opaque (the opacity-100 in the leaveFrom phase), and finish as completely transparent (the opacity-0 in the leaveTo phase).

All of these props are optional, and will default to just an empty string.

Co-ordinating multiple transitions

Sometimes you need to transition multiple elements with different animations but all based on the same state. For example, say the user clicks a button to open a sidebar that slides over the screen, and you also need to fade-in a background overlay at the same time.

You can do this by wrapping the related elements with a parent Transition component, and wrapping each child that needs its own transition styles with a Transition.Child component, which will automatically communicate with the parent Transition and inherit the parent's show state.

import { Transition } from '@headlessui/react' function Sidebar({ isShowing }) { return ( /* The `show` prop controls all nested `Transition.Child` components. */ {/* Background overlay */} {/* ... */} {/* Sliding sidebar */} {/* ... */} ) }

The Transition.Child component has the exact same API as the Transition component, but with no show prop, since the show value is controlled by the parent.

Parent Transition components will always automatically wait for all children to finish transitioning before unmounting, so you don't need to manage any of that timing yourself.

Transitioning on initial mount

If you want an element to transition the very first time it's rendered, set the appear prop to true.

This is useful if you want something to transition in on initial page load, or when its parent is conditionally rendered.

import { Transition } from '@headlessui/react' function MyComponent({ isShowing }) { return ( {/* Your content goes here*/} ) } Component APITransition PropDefaultDescriptionshow—Boolean

Whether the children should be shown or hidden.

asdivString | Component

The element or component to render in place of the Transition itself.

appearfalseBoolean

Whether the transition should run on initial mount.

unmounttrueBoolean

Whether the element should be unmounted or hidden based on the show state.

enter—String

Classes to add to the transitioning element during the entire enter phase.

enterFrom—String

Classes to add to the transitioning element before the enter phase starts.

enterTo—String

Classes to add to the transitioning element immediately after the enter phase starts.

entered—String

Classes to add to the transitioning element once the transition is done. These classes will persist after that, until it's time to leave.

leave—String

Classes to add to the transitioning element during the entire leave phase.

leaveFrom—String

Classes to add to the transitioning element before the leave phase starts.

leaveTo—String

Classes to add to the transitioning element immediately after the leave phase starts.

beforeEnter—() => void

Callback which is called before we start the enter transition.

afterEnter—() => void

Callback which is called after we finished the enter transition.

beforeLeave—() => void

Callback which is called before we start the leave transition.

afterLeave—() => void

Callback which is called after we finished the leave transition.

Transition.Child PropDefaultDescriptionasdivString | Component

The element or component to render in place of the Transition itself.

appearfalseBoolean

Whether the transition should run on initial mount.

unmounttrueBoolean

Whether the element should be unmounted or hidden based on the show state.

enter—String

Classes to add to the transitioning element during the entire enter phase.

enterFrom—String

Classes to add to the transitioning element before the enter phase starts.

enterTo—String

Classes to add to the transitioning element immediately after the enter phase starts.

entered—String

Classes to add to the transitioning element once the transition is done. These classes will persist after that, until it's time to leave.

leave—String

Classes to add to the transitioning element during the entire leave phase.

leaveFrom—String

Classes to add to the transitioning element before the leave phase starts.

leaveTo—String

Classes to add to the transitioning element immediately after the leave phase starts.

beforeEnter—() => void

Callback which is called before we start the enter transition.

afterEnter—() => void

Callback which is called after we finished the enter transition.

beforeLeave—() => void

Callback which is called before we start the leave transition.

afterLeave—() => void

Callback which is called after we finished the leave transition.

Styled examples

If you're interested in predesigned component examples using Headless UI and Tailwind CSS, check out Tailwind UI — a collection of beautifully designed and expertly crafted components built by us.

It's a great way to support our work on open-source projects like this and makes it possible for us to improve them and keep them well-maintained.

Explore more predesigned examples→


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有